import { ref } from 'vue'
import { defineStore } from 'pinia'

const useUserStore = defineStore('user', () => {
  const token = ref(sessionStorage.getItem('token') || '')
  const user = ref(JSON.parse(sessionStorage.getItem('user')) || {})

  const updateToken = (value) => {
    token.value = value
    sessionStorage.setItem('token', value)
  }

  const updateUser = (value) => {
    user.value = value
    sessionStorage.setItem('user', JSON.stringify(value))
  }

  const deleteToken = () => {
    token.value = ''
    sessionStorage.removeItem('token')
  }

  const deleteUser = () => {
    user.value = {}
    sessionStorage.removeItem('user')
  }

  return {
    token, user,
    updateToken, deleteToken, updateUser, deleteUser
  }
})

export default useUserStore